//
//  JLCategoryController.m
//  Liang美团HD
//
//  Created by sun on 16/1/20.
//  Copyright © 2016年 sun. All rights reserved.
//

#import "JLCategoryController.h"
#import "JLData.h"
#import "JLCategories.h"
#import "JLLeftViewCell.h"
#import "JLRightViewCell.h"
@interface JLCategoryController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *leftTableView;
@property (weak, nonatomic) IBOutlet UITableView *rightTableView;

@end

@implementation JLCategoryController

- (void)viewDidLoad {
    [super viewDidLoad];

    CGFloat rowHeight = 40;
    self.leftTableView.rowHeight = rowHeight;
    self.rightTableView.rowHeight = rowHeight;
    self.preferredContentSize = CGSizeMake(400, [JLData categoriesArray].count * rowHeight);
   
}

#pragma mark /********************tableView数据源方法********************/

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.leftTableView == tableView) {
        return [JLData categoriesArray].count;
    }else{
        NSUInteger row = [self.leftTableView indexPathForSelectedRow].row;
        JLCategories * cate = [JLData categoriesArray][row];
        return  cate.subcategories.count;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.leftTableView == tableView) {
        JLLeftViewCell * cell = [JLLeftViewCell cellWithTableView:tableView];
        JLCategories * category = [JLData categoriesArray][indexPath.row];
        cell.accessoryType = category.subcategories ? UITableViewCellAccessoryDisclosureIndicator: UITableViewCellAccessoryNone;
        cell.imageView.image = [UIImage imageNamed:category.small_icon];
        cell.imageView.highlightedImage = [UIImage imageNamed:category.small_highlighted_icon];
        cell.textLabel.text = category.name;
        return cell;
    }else{
        JLRightViewCell * cell =[JLRightViewCell cellWithTableView:tableView];
        NSUInteger row = [self.leftTableView indexPathForSelectedRow].row;
         JLCategories * category = [JLData categoriesArray][row];
        cell.textLabel.text = category.subcategories[indexPath.row];
        return cell;
    }
    
}





#pragma mark /********************tableView代理方法********************/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.leftTableView == tableView) {
        [self.rightTableView reloadData];
         JLCategories * cate = [JLData categoriesArray][indexPath.row];
        if (cate.subcategories == nil) {
            [self postNotificationWithObject:cate userInfo:nil];
           }
        }else{
            NSUInteger leftRow = [self.leftTableView indexPathForSelectedRow].row;
            JLCategories * cate = [JLData categoriesArray][leftRow];
            NSDictionary * dict = @{JLCategoryIndexFromButtonDidClickKey:@(indexPath.row)};
            [self postNotificationWithObject:cate userInfo:dict];
    }
}
- (void)postNotificationWithObject:(id)object userInfo:(NSDictionary *)userInfo{
    [[NSNotificationCenter defaultCenter] postNotificationName:JLCategoryButtonDidClickNotification object:object userInfo:userInfo];
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
